<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="任务名称" prop="taskName">
        <el-input v-model="queryParams.taskName" placeholder="请输入任务名称" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="群体" prop="ifGroup">
        <el-select v-model="queryParams.ifGroup" placeholder="请选择营销任务类型" clearable size="small">
          <el-option v-for="dict in ifGroupOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>

    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['buss:sqzxt:add']">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="taskInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="taskId" />
      <el-table-column label="任务名称" align="center" prop="taskName" />
      <el-table-column label="目标客户类型" align="center" prop="ifGroup" :formatter="ifGroupFormat" />
      <el-table-column label="客户经理" align="center" prop="userId" :formatter="userIdFormat" />
      <el-table-column label="机构ID" align="center" prop="orgId" :formatter="orgIdFormat" />
      <el-table-column label="任务开始时间" align="center" prop="beginDate" />
      <el-table-column label="任务结束时间" align="center" prop="endDate" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="添加时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['buss:marketTask:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['buss:marketTask:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改营销任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="营销任务类型(0总行，1个人)" prop="taskType">
          <el-select v-model="form.taskType" placeholder="请选择营销任务类型(0总行，1个人)">
            <el-option v-for="dict in taskTypeOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态  0不正常  1正常">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictValue">{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="任务来源0普通1是首页(首页列如生日提醒,理财到期)" prop="taskSytype">
          <el-select v-model="form.taskSytype" placeholder="请选择任务来源0普通1是首页(首页列如生日提醒,理财到期)">
            <el-option v-for="dict in taskSytypeOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listTaskInfo,
    getTaskInfo,
    delTaskInfo,
    addTaskInfo,
    updateTaskInfo
  } from "@/api/buss/marketTask";

  export default {
    name: "TaskInfo",
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 营销任务表格数据
        taskInfoList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // ID字典
        taskIdOptions: [],
        // 任务名称字典
        taskNameOptions: [],
        // 营销任务类型(0总行，1个人)字典
        taskTypeOptions: [],
        // 目标客户类型字典
        ifGroupOptions: [],
        // 任务开始时间字典
        beginDateOptions: [],
        // 任务结束时间字典
        endDateOptions: [],
        // 已营销客户数字典
        finishNumOptions: [],
        // 客户经理字典
        userIdOptions: [],
        // 添加时间字典
        createTimeOptions: [],
        // 修改时间字典
        updateTimeOptions: [],
        // 添加人字典
        createUserOptions: [],
        // 操作员字典
        updateUserOptions: [],
        // 状态  0不正常  1正常字典
        statusOptions: [],
        // 备注字典
        remarkOptions: [],
        // 营销目标字典
        taskTargetOptions: [],
        // 机构ID字典
        orgIdOptions: [],
        // 营销状态  0未完成  1已完成字典
        completeOptions: [],
        // 任务来源0普通1是首页(首页列如生日提醒,理财到期)字典
        taskSytypeOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          taskName: null,
          taskType: null,
          ifGroup: null,
          beginDate: null,
          endDate: null,
          finishNum: null,
          userId: null,
          createTime: null,
          updateTime: null,
          createUser: null,
          updateUser: null,
          status: null,
          remark: null,
          taskTarget: null,
          orgId: null,
          complete: null,
          taskSytype: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
      
      this.getDicts("ifGroup").then(response => {
        this.ifGroupOptions = response.data;
      });
      
      this.getDicts("sys_user_id").then(response => {
        this.userIdOptions = response.data;
      });
      
      this.getDicts("sys_org_id").then(response => {
        this.orgIdOptions = response.data;
      });
      
    },
    methods: {
      /** 查询营销任务列表 */
      getList() {
        this.loading = true;
        listTaskInfo(this.queryParams).then(response => {
          this.taskInfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // ID字典翻译
      taskIdFormat(row, column) {
        return this.selectDictLabel(this.taskIdOptions, row.taskId);
      },
      // 任务名称字典翻译
      taskNameFormat(row, column) {
        return this.selectDictLabel(this.taskNameOptions, row.taskName);
      },
      // 营销任务类型(0总行，1个人)字典翻译
      taskTypeFormat(row, column) {
        return this.selectDictLabel(this.taskTypeOptions, row.taskType);
      },
      // 目标客户类型字典翻译
      ifGroupFormat(row, column) {
        return this.selectDictLabel(this.ifGroupOptions, row.ifGroup);
      },
      // 任务开始时间字典翻译
      beginDateFormat(row, column) {
        return this.selectDictLabel(this.beginDateOptions, row.beginDate);
      },
      // 任务结束时间字典翻译
      endDateFormat(row, column) {
        return this.selectDictLabel(this.endDateOptions, row.endDate);
      },
      // 已营销客户数字典翻译
      finishNumFormat(row, column) {
        return this.selectDictLabel(this.finishNumOptions, row.finishNum);
      },
      // 客户经理字典翻译
      userIdFormat(row, column) {
        return this.selectDictLabel(this.userIdOptions, row.userId);
      },
      // 添加时间字典翻译
      createTimeFormat(row, column) {
        return this.selectDictLabel(this.createTimeOptions, row.createTime);
      },
      // 修改时间字典翻译
      updateTimeFormat(row, column) {
        return this.selectDictLabel(this.updateTimeOptions, row.updateTime);
      },
      // 添加人字典翻译
      createUserFormat(row, column) {
        return this.selectDictLabel(this.createUserOptions, row.createUser);
      },
      // 操作员字典翻译
      updateUserFormat(row, column) {
        return this.selectDictLabel(this.updateUserOptions, row.updateUser);
      },
      // 状态  0不正常  1正常字典翻译
      statusFormat(row, column) {
        return this.selectDictLabel(this.statusOptions, row.status);
      },
      // 备注字典翻译
      remarkFormat(row, column) {
        return this.selectDictLabel(this.remarkOptions, row.remark);
      },
      // 营销目标字典翻译
      taskTargetFormat(row, column) {
        return this.selectDictLabel(this.taskTargetOptions, row.taskTarget);
      },
      // 机构ID字典翻译
      orgIdFormat(row, column) {
        return this.selectDictLabel(this.orgIdOptions, row.orgId);
      },
      // 营销状态  0未完成  1已完成字典翻译
      completeFormat(row, column) {
        return this.selectDictLabel(this.completeOptions, row.complete);
      },
      // 任务来源0普通1是首页(首页列如生日提醒,理财到期)字典翻译
      taskSytypeFormat(row, column) {
        return this.selectDictLabel(this.taskSytypeOptions, row.taskSytype);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          taskId: null,
          taskName: null,
          taskType: null,
          ifGroup: null,
          beginDate: null,
          endDate: null,
          finishNum: null,
          userId: null,
          createTime: null,
          updateTime: null,
          createUser: null,
          updateUser: null,
          status: "0",
          remark: null,
          taskTarget: null,
          orgId: null,
          complete: null,
          taskSytype: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.taskId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加营销任务";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const taskId = row.taskId || this.ids
        getTaskInfo(taskId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改营销任务";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.taskId != null) {
              updateTaskInfo(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addTaskInfo(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const taskIds = row.taskId || this.ids;
        this.$confirm('是否确认删除营销任务编号为"' + taskIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTaskInfo(taskIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('buss/marketTask/export', {
          ...this.queryParams
        }, `buss_taskInfo.xlsx`)
      }
    }
  };
</script>
